<script setup lang="ts">
  import { ref, watch } from 'vue';
  import { addDir } from '@/api/dashboard';
  import { FormInstance, Message } from '@arco-design/web-vue';
  import useLoading from '@/hooks/loading';
  const formRef = ref<FormInstance | null>(null);
  const props = defineProps({
    visible: Boolean,
    formType: Array,
    params: Object,
  });
  const { loading, setLoading } = useLoading();
  defineOptions({
    name: 'menuOPtionAdd',
  });

  watch(
    () => props.formType,
    () => {},
    {
      deep: true,
      immediate: true,
    }
  );

  const formData = ref({
    dir_pid: props.params?.id ? props.params?.id : '', //上级目录id
    dir_name: '', //目录名称 *
    dir_type: '', //目录类型 1-公共目录 2-个人目录 3-归档目录 *
    dir_sort: '', // 排序
  });
  const emit = defineEmits(['update:visible', 'refresh']);

  const handleSubmit = async () => {
    if (!(await formRef.value?.checkForm())) {
      try {
        setLoading(true);
        const res = await addDir({
          ...formRef.value?.formModel,
          dir_type: props.params?.type,
        });
        Message.success(res.msg);
        emit('update:visible', false);
        emit('refresh');
        setLoading(false);
      } catch (error) {
        setLoading(false);
      }
    } else {
      Message.error('请正确填写表单！');
    }
  };
</script>

<template>
  <a-modal
    title="添加目录"
    :visible="visible"
    @ok="handleSubmit"
    title-align="start"
    :ok-loading="loading"
    @cancel="
      () => {
        emit('update:visible', false);
      }
    "
  >
    <pro-form
      ref="formRef"
      hiddenBtn
      :form-json="formType"
      :form-data="formData"
    ></pro-form>
  </a-modal>
</template>

<style scoped lang="less">
  :deep(.arco-select-view-inner) {
    text-align: left !important;
  }
  .menu2 {
    height: 100%;
    box-sizing: border-box;
    padding: 10px;
    cursor: default !important;
    background: #f7f8fa;
  }
  :deep(.icon) {
    width: 1.2em !important;
    height: 1.2em !important;
  }

  :deep(.arco-tree-node-title-text) {
    font-size: 12px;
    width: 140px;
    overflow: hidden; /* 隐藏溢出内容 */
    white-space: nowrap; /* 禁止换行 */
    text-overflow: ellipsis; /* 显示省略号 */
  }

  :deep(.arco-tree-node-indent-block) {
    width: 0px !important;
  }

  :deep(.arco-tree-node-indent) {
  }
</style>
